package van.planifolia.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created by Intellij IDEA<br>
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/11/9 16:37
 */
public class MakeLog {
    /**
     * 保存日志的工具类
     * @param logMsg 要保存的日志消息
     */
    public static void makeLog(String logMsg){
        File dir = new File("log");
        if (!dir.exists()){
            dir.mkdirs();
        }
        File file = new File("log/log.txt");
        FileOutputStream fos = null;
        try {
            // 关联文件流
            fos = new FileOutputStream(file,true);
            // 将日志以及日期推上去
            fos.write(logMsg.getBytes(StandardCharsets.UTF_8));
            fos.write("  ".getBytes(StandardCharsets.UTF_8));
            LocalDateTime localDateTime = LocalDateTime.now();
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
            String nowTime = localDateTime.format(dtf);
            fos.write("  ".getBytes(StandardCharsets.UTF_8));
            fos.write(nowTime.getBytes(StandardCharsets.UTF_8));
            fos.write("\n".getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                assert fos != null;
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
